package cn.bxshey.strategyModule.message;

import cn.bxshey.configModule.ws.SessionBean;
//import cn.bxshey.configModule.ws.WsHandler;
import cn.bxshey.configModule.ws.WsHandler;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @作者：不想说话而已
 * @Date：2024/3/10 1:17
 * 文本信息策略
 */
@Service
@Transactional(rollbackFor = IOException.class) //捕捉包括IO流的异常
public class TextStrategy implements MessageStrategy{

    @Resource
    WsHandler wsHandler;

    /**
     * @param msg 消息集合信息
     * @param id 接受者用户id
     * @return 是否成功
     */
    @Override
    public boolean sendMsg(Map<String, Object> msg,String id) throws IOException {
        System.err.println("执行");
        Map<String, SessionBean> sessionBeanMap = wsHandler.getSessionBeanMap();
        for(String key:sessionBeanMap.keySet()){
            sessionBeanMap.get(key).getSession().sendMessage(new TextMessage(JSON.toJSONString("测试消息")));
        }
        return false;
    }


    //初始化方法自动执行
    @Override
    public void afterPropertiesSet() throws Exception {
         //参数一策略名称，参数，策略接口
         MessageStrategyFactory.register("str",this);
    }
}
